package com.szrt.ssm.java.io.example03;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * 
 * @author huanghoulin
 *
 */
public class RandomAccessFileDemo {
	
	public static void write(String str,String path){
		
		
		RandomAccessFile f=null;  
        try {  
            f = new RandomAccessFile("thread/helloword.txt", "rw");  
            long length = f.length(); //获取文件的长度即字节数  
            //将写文件指针移到文件尾  
            f.seek(length);  
            if(length !=0){
            	f.write("\r\n".getBytes());  
            }
            f.write(str.getBytes());  
        }catch (Exception e) {  
            e.printStackTrace();  
        }finally{
        	//关闭流  
            if(null != f){
            	try {
					f.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
            }
        } 
	}
	
	public static void clear(){
		File f = new File("thread/helloword.txt");
		try {
			FileWriter fw =  new FileWriter(f);
			fw.write("");
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
